﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FastReport;

namespace MaterialPrint
{
    public partial class Form1 : Form
    {
        private Report report = new Report();
        enum OutputType {otPrint, otExport};

        public Form1()
        {
            InitializeComponent();

            toolStripStatusLabel1.Text = String.Format("Ver: {0}", System.Windows.Forms.Application.ProductVersion);
        }

        private void btSpotrebaCas_Click(object sender, EventArgs e)
        {
            DateRange dr = new DateRange();

            dr.DateFrom = DateTime.Now.AddDays(-2);
            dr.DateTo = DateTime.Now.AddDays(1);

            switch (dr.ShowDialog())
            {
                case System.Windows.Forms.DialogResult.OK:
                    PrintReport("SpotrebaCas.frx", dr.DateFrom, dr.DateTo);
                    break;
                case System.Windows.Forms.DialogResult.Yes:
                    PrintReport("SpotrebaCas.frx", dr.DateFrom, dr.DateTo, OutputType.otExport);
                    break;
            }
        }

        private void PrintReport(string FileName, DateTime dateFrom, DateTime dateTo, OutputType reportType = OutputType.otPrint)
        {
            report.Load(FileName);

            report.Dictionary.Connections[0].CommandTimeout = 120; // 2 minutes timeout (parameter)
            report.Dictionary.Connections[0].Enabled = false;
            report.Dictionary.Connections[0].ConnectionString = GetConnectionString();
            report.Dictionary.Connections[0].Enabled = true;

            MessageBox.Show(report.Dictionary.Connections[0].ConnectionString);

            report.SetParameterValue("@lDateFrom", dateFrom);
            report.SetParameterValue("@lDateTo", dateTo);

            report.Prepare();

            if (reportType == OutputType.otPrint)
            {
                report.Show();
            }
            else
            {
                saveFileDialog1.FileName = String.Format("{0:yyMMdd}_{1:yyMMdd}.xls", dateFrom, dateTo);

                if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    report.Export(new FastReport.Export.OoXML.Excel2007Export(), saveFileDialog1.FileName);
                }
            }
        }

        private static string GetConnectionString()
        {
            return String.Format("Server = {0}; Database = {1}; User Id = {2}; Password = {3};", Common.Configuration.DBServer, Common.Configuration.DBName, "sa", "joomlamaN");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DateRange dr = new DateRange();

            dr.DateFrom = DateTime.Now.AddDays(-2);
            dr.DateTo = DateTime.Now.AddDays(1);

            switch (dr.ShowDialog())
            {
                case System.Windows.Forms.DialogResult.OK:
                    PrintReport("SpotrebaSmeny.frx", dr.DateFrom, dr.DateTo);
                    break;
                case System.Windows.Forms.DialogResult.Yes:
                    PrintReport("SpotrebaSmeny.frx", dr.DateFrom, dr.DateTo, OutputType.otExport);
                    break;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DateRange dr = new DateRange();

            dr.DateFrom = DateTime.Now.AddDays(-2);
            dr.DateTo = DateTime.Now.AddDays(1);

            switch (dr.ShowDialog())
            {
                case System.Windows.Forms.DialogResult.OK:
                    PrintReport("SpotrebaDny.frx", dr.DateFrom, dr.DateTo);
                    break;
                case System.Windows.Forms.DialogResult.Yes:
                    PrintReport("SpotrebaDny.frx", dr.DateFrom, dr.DateTo, OutputType.otExport);
                    break;
            }
        }
    }
}